//
//  GYShopingCartGoodsTableViewCell.m
//  GoodLuck
//
//  Created by apple on 2018/3/27.
//  Copyright © 2018年 yunjiazhineng. All rights reserved.
//


static NSString *shopingCartGoodsCellIdentifier = @"shopingCartGoodsCellIdentifier";

#import "GYShopingCartGoodsTableViewCell.h"
#import "GYNetWorkTools.h"

@interface GYShopingCartGoodsTableViewCell ()<UITextFieldDelegate>
@property (weak, nonatomic) IBOutlet UIImageView *goodsImageView;
@property (weak, nonatomic) IBOutlet UILabel *goodsNameLbale;
@property (weak, nonatomic) IBOutlet UILabel *goodsPriceLable;
@property (weak, nonatomic) IBOutlet UITextField *goodsNumTF;
@property (weak, nonatomic) IBOutlet UILabel *markLable;

@property (weak, nonatomic) IBOutlet UIButton *stateBtu;
/** 商品数量 */
@property (nonatomic, assign) NSInteger goodsCount;

@end

@implementation GYShopingCartGoodsTableViewCell

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
    self.goodsCount = 1;
    
    
}

+ (instancetype)cellWithTableView:(UITableView *)tableView {
    GYShopingCartGoodsTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:shopingCartGoodsCellIdentifier];
    if (cell == nil) {
        cell = [[[NSBundle mainBundle] loadNibNamed:NSStringFromClass([self class]) owner:nil options:nil] firstObject];
    }
    
    return cell;
}

- (void)setGoods:(GYShopingCartGoods *)goods {
    _goods  = goods;
    
    [self.goodsImageView sd_setImageWithURL:[NSURL URLWithString:goods.goods_image_url] placeholderImage:[UIImage imageNamed:@"4"]];
    self.goodsNameLbale.text = goods.goods_name;
    self.goodsPriceLable.text = [NSString stringWithFormat:@"￥%@",goods.goods_price];
    self.goodsNumTF.text = goods.goods_num;
    self.goodsCount = [goods.goods_num intValue];//设置购物车物品的数量
    self.stateBtu.selected = goods.isSelectState;
    
}

- (CGFloat)getHeight:(GYShopingCartGoods *)model {
    self.goods = model;
    
    [self layoutIfNeeded];
    
    return CGRectGetMaxY(self.markLable.frame);
}

//删除按钮点击事件
- (IBAction)deleteBtuClick:(UIButton *)sender {
    [GYNetWorkTools requestDeleteShopingCartGoods:self.goods.cart_id success:^(NSDictionary *obj) {
        int code = [[obj objectForKey:@"code"] intValue];
        if (code == 200) {
            [EasyTextView showSuccessText:@"删除成功!"];
            //通知外部 购物车数量标签更新
            [[NSNotificationCenter defaultCenter] postNotificationName:updateShopingCartGoodsNum object:nil];
            
            self.deleteOrAddGoodsNumCallBack(self.goodsCount);
        }else if (code == 400) {
            [EasyTextView showText:@"删除失败!"];
        }
    } error:^(NSError *error) {
        [EasyTextView showText:@"删除失败!"];
        NSLog(@"DeleteShopingCartGoodsFailed===%@",error);
    }];
}

- (IBAction)changedGoodsNum:(UIButton *)sender {
    [self.goodsNumTF resignFirstResponder];
    switch (sender.tag) {
        case 1:
        {
            if (self.goodsCount <= 1) {
                sender.enabled = NO;
            }else {
                sender.enabled = YES;
                self.goodsCount -= 1;
            }
        }
            break;
            
        case 2:
        {
            self.goodsCount += 1;
        }
            break;
            
        default:
            break;
    }
    
    //这里要调用网络接口来实现 购物车商品的增加和减少
    [GYNetWorkTools requestUpdateShopingCartGoodsNum:self.goods.cart_id quantity:[NSString stringWithFormat:@"%zd",self.goodsCount] success:^(NSDictionary *obj) {
        int code = [[obj objectForKey:@"code"] intValue];
        NSDictionary *datas = [obj objectForKey:@"datas"];
        if (code == 200) {
            self.goodsNumTF.text = [NSString stringWithFormat:@"%zd",self.goodsCount];
            self.deleteOrAddGoodsNumCallBack(self.goodsCount);
            //float total_price = [[datas objectForKey:@"total_price"] floatValue];
# warning 把该商品的总价格传递出去
        }
    } error:^(NSError *error) {
        NSLog(@"UpdateShopingCartGoodsNumFailed===%@",error);
    }];
    
}


- (void)textFieldDidEndEditing:(UITextField *)textField {
    if ([textField.text intValue] < 1) {
        self.goodsCount = 1;
        textField.text = @"1";
    }
    self.goodsCount = [textField.text intValue];
    self.deleteOrAddGoodsNumCallBack(self.goodsCount);
}

@end
